<template>
  <div style="width: 100%; height: 100vh; background-color: #cfcfcf; overflow: hidden; margin: 0px;">
    <div style="width: 400px; margin: 150px auto; border-radius: 10px; background-color: white; padding: 30px; shadow: 0 0 10px rgba(0, 0, 0, 0.2);"> 
      <div style="color: #cccccc; font-size: 30px; text-align: center;padding: 30px 0; color: black;">
        欢迎注册
      </div>
      <el-form ref="form" label-width="auto" style="max-width: 600px" :rules="rules" :model="form" label-position="left">
        <el-form-item label="用户名" prop="adminname">
          <el-input v-model="form.adminname" >
            <template #prefix>
              <el-icon class="el-input__icon"><avatar /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" show-password >
            <template #prefix>
              <el-icon class="el-input__icon"><lock /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="confirm">
          <el-input v-model="form.confirm" show-password >
            <template #prefix>
              <el-icon class="el-input__icon"><lock /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="register" style="width: 100%;">注册</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { Avatar, Lock } from '@element-plus/icons-vue'
import request from '../utils/request'

export default {
  components: { Avatar, Lock },
  data () {
    return {
      form: {
        adminname: '',
        password: '',
      },
      rules: {
        adminname: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
        ],
        confirm: [
          { required: true, message: '请再次确认密码', trigger: 'blur' },
        ],
      }
    }
  },


  methods: {
    register () {
      if (this.form.password === this.form.confirm) {
        this.$refs['form'].validate(valid => {
        if(valid) {
          console.log(this.form)
          request.post('http://localhost:8090/register', this.form).then(res => {
            if (res.code === 0) {
              this.$message({
                type: 'success',
                message: '注册成功',
              })
              this.$router.push({ path: '/login' })
            } else {
              this.$message({
                type: 'error',
                message: res.msg,
              })
            }
          })
        }
        })
      }
      else {
        this.$message({
          type: 'error',
          message: '两次密码不一致',
        })
      }
    }
  },
}
</script>

<style scoped>
</style>